package com.erainm.springbootkafka.config;

import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaAdmin;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by erainm on 2021/9/9 10:56 上午.
 *
 * @Description TODO:
 **/
@Configuration
public class KafkaConfig {

    @Bean
    public NewTopic topic1(){
        return new NewTopic("nptc-01",3,(short) 1);
    }

    @Bean
    public NewTopic topic2(){
        return new NewTopic("nptc-02",5,(short)1);
    }

    @Bean
    public KafkaAdmin kafkaAdmin(){
        Map<String,Object> configs = new HashMap<>();
        configs.put("bootstrap.servers","lg04:9092");
        KafkaAdmin admin = new KafkaAdmin(configs);
        return admin;
    }

    public KafkaTemplate<Integer,String> kafkaTemplate(ProducerFactory<Integer,String> factory){
        // 覆盖原有设置
        Map<String,Object> configOverride = new HashMap<>();
        configOverride.put(ProducerConfig.BATCH_SIZE_CONFIG,200);

        KafkaTemplate<Integer, String> template = new KafkaTemplate<>(factory, configOverride);
        return template;
    }
}
